package com.eve.eplus.sns;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.eve.eplus.common.app.Constants;
import com.eve.eplus.common.db.PersistentUtils;
import com.eve.eplus.model.AccountModel;

/**
 * 用于管理SNS帐号
 * 
 * @author norsie
 * @create 13-04-08
 */
public class SNSAccountManager
{
    // private Context mContext;
    // private AccountModel accountModel;

    public SNSAccountManager()
    {
        // this.mContext = context;
    }

    /**
     * 储存帐号信息
     * @param accountInfo
     */
    public static void save(HashMap<String, String> accountInfo)
    {
        AccountModel accountModel = new AccountModel();
        accountModel.setName(accountInfo.get(Constants.SNS_ACCOUNT_NAME));
        accountModel.setType(accountInfo.get(Constants.SNSTYPE));
        accountModel.setAccesstoken(accountInfo.get(Constants.ACCESS_TOKEN));
        accountModel.setExpires(accountInfo.get(Constants.EXPIRES_IN));
        accountModel.setOvertime(accountInfo.get(Constants.OVERTIME));
        accountModel.setUid(accountInfo.get(Constants.SNS_UID));
        PersistentUtils.addModel(accountModel);
    }
    
    /**
     * 通过token信息查询该帐号是否存在，同时需要传递帐号类型参数
     * @param token token信息
     * @param snsType 帐号类型
     * @return
     */
    public static HashMap<String, String> getAcountByToken(String token,String snsType){
        
        List<AccountModel> list = PersistentUtils.getModelList(
                AccountModel.class, "accesstoken='"+token+"' and type='"+snsType+"'");
        if(list.isEmpty())
            return null;
        else {
            HashMap<String, String> accountInfo = new HashMap<String, String>();
            accountInfo.put(Constants.SNS_ACCOUNT_NAME, list.get(0).getName());
            accountInfo.put(Constants.ACCESS_TOKEN,
                    list.get(0).getAccesstoken());
            accountInfo.put(Constants.EXPIRES_IN, list.get(0).getExpires());
            accountInfo.put(Constants.SNS_ACCOUNT_INDEX, list.get(0).getInx());
            accountInfo.put(Constants.SNSTYPE, list.get(0).getType());
            accountInfo.put(Constants.SNS_UID, list.get(0).getUid());
            return accountInfo;
        }
    }
    
    
    /**
     * 从数据库读取已经绑定的SNS帐号列表
     */
    public static ArrayList<HashMap<String, String>>  getAccountList()
    {
        ArrayList<HashMap<String, String>>  accountList=new ArrayList<HashMap<String,String>>();
        List<AccountModel> list = PersistentUtils.getModelList(
                AccountModel.class, null);
        for (int i = 0; i < list.size(); i++)
        {
            HashMap<String, String> accountInfo = new HashMap<String, String>();
            accountInfo.put(Constants.ACCOUNT_ID, list.get(i).getId());
            accountInfo.put(Constants.SNS_ACCOUNT_NAME, list.get(i).getName());
            accountInfo.put(Constants.ACCESS_TOKEN,
                    list.get(i).getAccesstoken());
            accountInfo.put(Constants.EXPIRES_IN, list.get(i).getExpires());
            accountInfo.put(Constants.SNS_ACCOUNT_INDEX, list.get(i).getInx());
            accountInfo.put(Constants.SNSTYPE, list.get(i).getType());
            accountInfo.put(Constants.SNS_UID, list.get(i).getUid());
            accountList.add(accountInfo);
        }
        return accountList;
    }
    
    /**
     * 删除SNS帐号
     */
    
    public static void delete(List<AccountModel> list) {
        for (AccountModel model : list) {
            PersistentUtils.delete(model);
        }
    }
}
